﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessClasses;

namespace TicketsOnline
{
    public partial class Admin : System.Web.UI.Page
    {
        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                
                //DataAccessLayer dal = new DataAccessLayer();
                //PurchasesListView.DataSource = dal.FindPurchases(null, OrderStatus.Unspecified);
                //PurchasesListView.DataBind();

                StatusFilterDropDown.Items.Add("Unspecified");
                StatusFilterDropDown.Items.Add("Ordered");
                StatusFilterDropDown.Items.Add("Delivered");

            }
            FilterButton_Click(sender,e);
             
        }

        protected void FilterButton_Click(object sender, EventArgs e)
        {
            DataAccessLayer dal = new DataAccessLayer();
            OrderStatus status = OrderStatus.Unspecified;

            if (StatusFilterDropDown.Text == "Ordered") status = OrderStatus.Ordered;
            if (StatusFilterDropDown.Text == "Delivered") status = OrderStatus.Delivered;

            int? refNo = null;
            int refNoResult;
            if (int.TryParse(RefNoFilterBox.Text, out refNoResult))
            {
                refNo = refNoResult;
            }


            PurchasesListView.DataSource = dal.FindPurchases(refNo, status);
            PurchasesListView.DataBind();
        }

        protected void LinkButton_Clicked(object sender, EventArgs e)
        {
            LinkButton btn = (sender as LinkButton);

            DataAccessLayer dal = new DataAccessLayer();
            OrderStatus status = OrderStatus.Unspecified;

            if (btn.Text.Contains("Ordered"))
            {
                status = OrderStatus.Ordered;
            }
            
            if(btn.Text.Contains("Delivered"))
            {
                status = OrderStatus.Delivered;
            }
            
            if (status == OrderStatus.Unspecified)
            {
                return;
            }

          
            dal.ChangePurchaseStatus( int.Parse(btn.CommandArgument) , status);

            FilterButton_Click(sender, e);
        }
    }
}